package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.OrderInfo;
import com.mvp.shq.washer.rxbus.BusData;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.DateUtils;
import com.mvp.shq.washer.utils.EventCode;
import com.orhanobut.logger.Logger;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/21.
 */

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {

    private List<OrderInfo> orders;
    private Context context;

    public OrderAdapter(Context mContext,List<OrderInfo> list) {
        context = mContext;
        orders = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position == 0){
            holder.division.setVisibility(View.VISIBLE);
            holder.allOrderView.setVisibility(View.GONE);
            return;
        }
        OrderInfo order = orders.get(position - 1);
        holder.tvOrderNo.setText("订单 "+order.getOrderNo());
        Date d = DateUtils.StrToDate(order.getCreateDate());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd HH:mm");
        //格式化为日期/时间字符串
        String cc=sdf.format(d);
        holder.tvOrderTime.setText(cc);
        holder.tvOrderLocation.setText(order.getWasherAddress());
        holder.tvOrderNumber.setText("洗衣机"+order.getWasherNo());
        if ("3".equals(order.getPayType())){
            holder.tvMoneyUnit.setVisibility(View.GONE);
            holder.tvOrderMoney.setText("优惠券支付");
        }else {
            holder.tvMoneyUnit.setVisibility(View.VISIBLE);
            holder.tvOrderMoney.setText(order.getTotalMoney()+"");
        }
        holder.tvOrderModel.setText(order.getPatternName());
        String statusStr = "";
        if (DataDictionary.WASHER_ORDER_PAY_STATUS_PAY.equals(order.getPayStatus())){
            if (DataDictionary.WASHER_ORDER_STATUS_ERROR.equals(order.getStatus())){
                statusStr = "异常";
            }else if (DataDictionary.WASHER_ORDER_STATUS_NOT_PAY.equals(order.getStatus())){
                statusStr = "未支付";
            }else if (DataDictionary.WASHER_ORDER_STATUS_ING.equals(order.getStatus())){
                statusStr = "进行中";
            }else if (DataDictionary.WASHER_ORDER_STATUS_FINISH.equals(order.getStatus())){
                statusStr = "已完成";
            }
        }else if (DataDictionary.WASHER_ORDER_PAY_STATUS_REFUND_ING.equals(order.getPayStatus())){
            statusStr = "退款中";
        }else if (DataDictionary.WASHER_ORDER_PAY_STATUS_REFUND.equals(order.getPayStatus())){
            statusStr = "已退款";
        }
        holder.tvOrderState.setText(statusStr);
        holder.division.setVisibility(View.GONE);
        holder.allOrderView.setVisibility(View.VISIBLE);
        holder.allOrderView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Logger.d("点击item："+position);
                BusData data = new BusData();
                data.setPosition(position - 1);
                RxBus.get().send(EventCode.ORDER_ITEM_CLICK,data);
            }
        });
        int bgType = position%4;
        switch (bgType){
            case 0:
                holder.allOrderView.setBackgroundResource(R.drawable.order_item_1);
                break;
            case 1:
                holder.allOrderView.setBackgroundResource(R.drawable.order_item_2);
                break;
            case 2:
                holder.allOrderView.setBackgroundResource(R.drawable.order_item_3);
                break;
            case 3:
                holder.allOrderView.setBackgroundResource(R.drawable.order_item_4);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return orders.size() + 1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_order_no)
        TextView tvOrderNo;
        @BindView(R.id.tv_order_state)
        TextView tvOrderState;
        @BindView(R.id.tv_order_time)
        TextView tvOrderTime;
        @BindView(R.id.tv_order_location)
        TextView tvOrderLocation;
        @BindView(R.id.tv_order_number)
        TextView tvOrderNumber;
        @BindView(R.id.tv_order_model)
        TextView tvOrderModel;
        @BindView(R.id.tv_order_money)
        TextView tvOrderMoney;
        @BindView(R.id.tv_money_unit)
        TextView tvMoneyUnit;
        @BindView(R.id.all_order_view)
        AutoLinearLayout allOrderView;
        @BindView(R.id.division)
        View division;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            AutoUtils.autoSize(itemView);
        }
    }
}
